package com.tbit.main.util;

import cn.hutool.core.collection.CollUtil;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串实体类
 *
 * @author Leon
 * 2019年1月23日 下午3:57:03
 */
public class StringUtil {

    /**
     * 分解字符串(整形)
     */
    public static List<Integer> getIntegerList(String param) {
        if (param == null || "".equals(param)) {
            return new ArrayList<>();
        }

        String[] params = param.split(",");
        List<Integer> resultList = new ArrayList<Integer>();
        for (String par : params) {
            resultList.add(Integer.parseInt(par));
        }

        return resultList;
    }

    /**
     * 分解字符串(整形)
     */
    public static List<Integer> getIntegerList(String param, String regex) {
        if (param == null || "".equals(param)) {
            return new ArrayList<>();
        }

        String[] params = param.split(regex);
        List<Integer> resultList = new ArrayList<Integer>();
        for (String par : params) {
            resultList.add(Integer.parseInt(par));
        }

        return resultList;
    }

    /**
     * 分解字符串(Long)
     */
    public static List<Long> getLongList(String param) {
        String[] params = param.split(",");
        List<Long> resultList = new ArrayList<Long>();
        for (String parm : params) {
            resultList.add(Long.parseLong(parm));
        }

        return resultList;
    }

    /**
     * 分解字符串(String)
     */
    public static List<String> getStringList(String param) {
        String[] params = param.split(",");
        List<String> resultList = new ArrayList<String>();
        for (String parm : params) {
            resultList.add(param);
        }

        return resultList;
    }

    /**
     * 构造字符串
     */
    public static String getListString(Collection<Integer> ids) {
        if (CollUtil.isEmpty(ids)) {
            return "";
        }

        StringBuilder sb = new StringBuilder();
        for (Integer id : ids) {
            if (sb.length() > 0) {
                sb.append(",");
            }
            sb.append(id);
        }
        return sb.toString();
    }

    /**
     * 构造字符串
     */
    public static String getListString(List<Integer> ids) {
        if (ids == null || ids.size() == 0) {
            return null;
        }

        StringBuffer result = new StringBuffer();

        for (int i = 0, length = ids.size(); i < length; i++) {
            result.append(ids.get(i));

            if (i != (length - 1)) {
                result.append(",");
            }
        }

        return result.toString();
    }

    /**
     * 构造字符串
     */
    public static String getString(List<String> ids) {
        if (ids == null || ids.size() == 0) {
            return null;
        }

        StringBuffer result = new StringBuffer();

        for (int i = 0, length = ids.size(); i < length; i++) {
            result.append(ids.get(i));

            if (i != (length - 1)) {
                result.append(",");
            }
        }

        return result.toString();
    }

    /**
     * 构造字符串
     */
    public static String getLongListString(List<Long> ids) {
        StringBuffer result = new StringBuffer();

        for (int i = 0, length = ids.size(); i < length; i++) {
            result.append(ids.get(i));

            if (i != (length - 1) && length > 1) {
                result.append(",");
            }
        }

        return result.toString();
    }

    /**
     * 构造字符串
     */
    public static String getListStringLong(List<Long> ids) {
        StringBuffer result = new StringBuffer();

        for (int i = 0, length = ids.size(); i < length; i++) {
            result.append(ids.get(i));

            if (i != (length - 1)) {
                result.append(",");
            }
        }

        return result.toString();
    }

    /**
     * 构造字符串
     */
    public static String getMapString(HashMap<String, Integer> hashMap) {
        StringBuffer result = new StringBuffer();
        for (Integer value : hashMap.values()) {
            result.append(value + "，");
        }
        result.deleteCharAt(result.length() - 1);
        return result.toString();
    }

    /**
     * 构造字符串
     */
    public static String getArrayListString(List<String> paramList) {
        StringBuilder result = new StringBuilder();
        if (CollUtil.isEmpty(paramList)) {
            return result.toString();
        }

        for (int i = 0, length = paramList.size(); i < length; i++) {
            result.append("'").append(paramList.get(i)).append("'");

            if (i != (length - 1)) {
                result.append(",");
            }
        }

        return result.toString();
    }

    /**
     * 构造字符串
     */
    public static String getListToString(List<String> paramList) {
        StringBuffer result = new StringBuffer();

        for (int i = 0, length = paramList.size(); i < length; i++) {
            result.append(paramList.get(i));

            if (i != (length - 1)) {
                result.append("、");
            }
        }

        return result.toString();
    }

    /**
     * 构造字符串
     */
    public static String getStringListString(List<String> paramList) {
        StringBuffer result = new StringBuffer();

        for (int i = 0, length = paramList.size(); i < length; i++) {
            result.append("'");
            result.append(paramList.get(i));

            if (i != (length - 1)) {
                result.append("',");
            }
        }
        result.append("'");

        return result.toString();
    }


    /**
     * 构造HTTP字符串
     */
    public static String getArrayListStringHttp(List<String> paramList) {
        StringBuffer result = new StringBuffer();

        for (int i = 0, length = paramList.size(); i < length; i++) {
            result.append(paramList.get(i));

            if (i != (length - 1)) {
                result.append(",");
            }
        }

        return result.toString();
    }

    /**
     * 分解字符串（double）
     */
    public static List<Double> getDoubleList(String param) {
        String[] params = param.split(",");
        List<Double> resultList = new ArrayList<Double>();
        for (String par : params) {
            resultList.add(Double.parseDouble(par));
        }

        return resultList;
    }

    /**
     * 获取六位随机数
     *
     * @return
     */
    @SuppressWarnings("unchecked")
    public static String getAuthCode() {
        String str = "0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        @SuppressWarnings("rawtypes")
        List list = new ArrayList();
        while (sb.length() < 4) {
            int number = random.nextInt(10);
            if (sb.length() > 0 && !"0".equals(sb.substring(0, 1))) {
                int n = 0;
                for (int i = 0; i < list.size(); i++) {
                    if (!((Object) number).equals(list.get(i))) {
                        n++;
                    }
                }
                if (n == sb.length()) {
                    sb.append(str.charAt(number));
                    list.add(number);
                }
            } else {
                if (!"0".equals(String.valueOf(str.charAt(number)))
                        && sb.length() == 0) {
                    sb.append(str.charAt(number));
                    list.add(number);
                }
            }
        }

        return sb.toString();
    }

    /**
     * 判断字符串是否为数字
     *
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }

    /**
     * 判断是不是手机号码
     *
     * @param mobiles
     * @return
     */
    public static boolean isMobileNO(String mobiles) {
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    /**
     * 获取uuid
     */
    public static String getUUID() {
        return UUID.randomUUID().toString();
    }

    /**
     * 获取骑行订单号
     *
     * @return
     */
    public static String getOrderNO() {
        // 10位随机数
        return "THCS" + DateTimeUtil.getNowTimeyyyyMMdd() + getRandomString(10);
    }

    /**
     * 获取订单号
     *
     * @return
     */
    public static String getRideLogOrderNO() {
        // 10位随机数
        return DateTimeUtil.getNowTimeS1() + getRandomString(10);
    }

    /**
     * 获取指定位数随机数
     *
     * @param number
     * @return
     */
    public static String getRandomString(int number) {
        StringBuilder result = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < number; i++) {
            result.append(random.nextInt(10));
        }

        return result.toString();
    }

    public static void main(String[] args) {
    }

}
